import Vuex from "vuex";
import router from "@/router/index.js";
import request from "@/utils/request.js";
import utils from "@/store/modules/utils.js"

export const store = new Vuex.Store({
    // state专门用来保存 共享的状态值
    state: {
        // 单位搜索
        searchClientName:"",
        tabList:[],
        // 侧边栏显隐控制变量
        isCollapse: false,
        mapDialog:false,
        announceDialog:false,
        warnDialog:false,
        pointClickDialog:true,
        currentClientInfo: {},// 首页切换单位
        treeData:[], // 组织树节点
        pointClickDialogData:[],
    },
    // mutations: 专门书写方法,用来更新 state 中的值
    mutations: {
        setPointClickDialogData:(state, pointClickDialogData)=>{
          state.pointClickDialogData=pointClickDialogData;
        },
        updateCurrentClientInfo: (state, pointInfo) => {
            pointInfo.pointMap = pointInfo.pointMap ? pointInfo.pointMap.split(',') : [];
            state.currentClientInfo=pointInfo
        },
        addTab: (state, tab) => {
            // 如果tab已经存在，不添加新的tabs
            if (state.tabList.some(item => item.path === tab.path)) return
            state.tabList.push(tab)
        },
        // 删除tab
        delete_tabs (state, route) {
            let index = 0
            for (let gohh of state.tabList) {
                if (gohh.path === route) {
                    break
                }
                index++
            }
            this.state.tabList.splice(index,1)
            if (state.tabList.length>0){
                router.push(state.tabList.at(state.tabList.length-1))
            }
        },
        doCollapse(state) {
            state.isCollapse = !state.isCollapse;
        },
        resetOrganization(state){
            state.treeData=[]
            try {
                request.get("/organization/" + JSON.parse(localStorage.getItem("user")).organization).then(res=>{
                    if (res.data) {
                        state.treeData.push(res.data)
                    } else {
                        this.$message.error("无数据");
                    }
                })
            } catch (error) {
                this.$message.error("获取组织失败" + error);
            }
        },
    },
    actions: {
    },
    modules: {
        utils,
    }
});

// 整合初始状态和变更函数，我们就得到了我们所需的 store
// 至此，这个 store 就可以连接到我们的应用中
export default store
